Nullable

Defines a value paired with a distinctive "null" state that denotes the absence of a value. If default constructed, a Nullable!T object starts in the null state. Assigning it renders it non-null. Calling nullify can nullify it again. Practically Nullable!T stores a T and a bool.

Constructors

this
this(T value)

Constructor initializing this with value.

this
this(N n)
Undocumented in source.

Alias This

get

Implicitly converts to T. this must not be in the null state.

Members

Functions

getValueOr
inout(T) getValueOr(T defVal)

Gets the value or the default value passed in.

nullify
void nullify()

Forces this to the null state.

opAssign
void opAssign(T value)
void opAssign(N n)

Assigns value to the internally-held state. If the assignment succeeds, this becomes non-null.

opCmp
int opCmp(N n)
Undocumented in source. Be warned that the author may not have intended to support it.
opCmp
int opCmp(Nullable!T rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opCmp
int opCmp(T rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(N n)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(T rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(Nullable!T rhs)
Undocumented in source. Be warned that the author may not have intended to support it.

Properties

get
inout(T) get [@property getter]

Gets the value. this must not be in the null state. This function is also called for the implicit conversion to T.

isNull
bool isNull [@property getter]

Check if this is in the null state.

Templates

toString
template toString()
Undocumented in source.

Examples

struct CustomerRecord
{
    string name;
    string address;
    int customerNum;
}

Nullable!CustomerRecord getByName(string name)
{
    //A bunch of hairy stuff

    return Nullable!CustomerRecord.init;
}

auto queryResult = getByName("Doe, John");
if (!queryResult.isNull)
{
    //Process Mr. Doe's customer record
    auto address = queryResult.address;
    auto customerNum = queryResult.customerNum;

    //Do some things with this customer's info
}
else
{
    //Add the customer to the database
}

See Also

Meta